// pages/mine/order/order.js
var uid
Page({

  
  data: {
    more:'0%', change:1, ding:1,nomoneylist:[],order:1,noprintlist:[],comlelist:[],tuimoney:[],everylist:[],
    num:'',page:1,value:1,index1:0,limit:10,value5:'',passwordvalue:'',total:'',last:'',show4:false,
    appId:'', 
    nonceStr:'',
    package:'',
    signType:'',flag:false,
    timeStamp:'',show:false,code:''
  },

  onShow(){
       this.getlist(uid,this.data.order,1,this.data.limit)
  },
  onLoad: function (options) {
    
    
    var more = (options.change-1)*20;
    more = more+'%';
    this.setData({
      change:options.change,
      more:more,
      ding:options.change,
      order:options.change
    })

    var that = this
     uid = wx.getStorageSync('uid');
    wx.request({
      url: 'https://zujieba.com/api/order/mine_order.html', //仅为示例，并非真实的接口地址
      data: {
        uid:uid,
        type:that.data.change,
        page:1, 
        limit:this.data.limit,
      },
      success: function(res){
        console.log(res);
        console.log(typeof(res.data.data.num));
        
        that.setData({num:res.data.data.num?res.data.data.num:0})
        if(options.change==1){ that.setData({
          nomoneylist:res.data.data.data
        })}else if(options.change==2){
          console.log(res);
          
          that.setData({noprintlist:res.data.data.data})
        } else if(options.change==3){
          that.setData({comlelist:res.data.data.data})
        }else if(options.change==4){
          that.setData({tuimoney:res.data.data.data})
        }else{
          that.setData({everylist:res.data.data.data})
        }
        
       
        }
    })

    console.log(this.data.change);
    // var query = wx.createSelectorQuery();
    // //选择id
    // query.select('.order_content').boundingClientRect()
    // query.exec(function(res) {
    //     //res就是 所有标签为mjltest的元素的信息 的数组
    //     console.log(res);
    // })

  },


  dian:function(e){
    this.setData({
   order:e.currentTarget.dataset.order,
   page:1,
   value:1
    })

    
    var order = e.currentTarget.dataset.order;
    if(this.data.change == order){
      return false;
    }else{
      var dong = 0;
      dong = (order-this.data.ding)*20;
      dong = dong+'%';
      // console.log(order);
      this.setData({
        change:order,
        // more:dong
      })
    }
    
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease',
    });
    var width= wx.getSystemInfoSync().windowWidth;
    width = (width/5)*(order-this.data.ding);
    // return false;
    animation.translate(width,0).step()
    this.setData({
      ani:animation.export()
    })
    /*this.getlist(wx.getStorageSync('uid'),this.data.change)*/
    this.getlist(uid,e.currentTarget.dataset.order,1,this.data.limit)
     
  },
  dian1:function(e){
    this.setData({
   order:e,
   page:1,
   value:1
    })
  console.log(22222);
  
    
    var order = e
    if(this.data.change == order){
      return false;
    }else{
      var dong = 0;
      dong = (order-this.data.ding)*20;
      dong = dong+'%';
      // console.log(order);
      this.setData({
        change:order,
        // more:dong
      })
    }
    
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease',
    });
    var width= wx.getSystemInfoSync().windowWidth;
    width = (width/5)*(order-this.data.ding);
    // return false;
    animation.translate(width,0).step()
    this.setData({
      ani:animation.export()
    })
    /*this.getlist(wx.getStorageSync('uid'),this.data.change)*/
    console.log(3333);
    
    this.getlist(uid,e,1,this.data.limit)
     
  },
  getlist(uid,type,page,limit){
     
    wx.request({
      url: 'https://zujieba.com/api/order/mine_order.html',
      data:{uid,type,page,limit},
      success:(res)=>{ 
        console.log(res);
        this.setData({num:res.data.data.num})
       console.log(res.data.data.data.length,this.data.num);
    /*   if(res.data.data.data.length==0){  return }*/
        
      console.log(typeof(res.data.data.data))
        if(type==1){ 
          if(page==1){
            this.setData({nomoneylist:res.data.data.data})
          }else{
          console.log(res);
          this.setData({
            nomoneylist:this.data.nomoneylist.concat(res.data.data.data)
          })}
        }else if(type==2){
          if(page==1){
            this.setData({noprintlist:res.data.data.data})
          }else{
          console.log(res);
          this.setData({
            noprintlist:this.data.noprintlist.concat(res.data.data.data)
          })}
        } else if(type==3){
          if(page==1){
            this.setData({comlelist:res.data.data.data})
          }else{
          console.log(res);
          this.setData({
            comlelist:this.data.comlelist.concat(res.data.data.data)
          })}
        }else if(type==4){
          if(page==1){
            this.setData({tuimoney:res.data.data.data})
          }else{
          console.log(res);
          this.setData({
            tuimoney:this.data.tuimoney.concat(res.data.data.data)
          })}
        }else{
          if(page==1){
            this.setData({everylist:res.data.data.data})
          }else{
          console.log(res);
          this.setData({
            everylist:this.data.everylist.concat(res.data.data.data)
          })}
          
        }
       
           
     
      }
    })
  },
   //支付
   getpay(code){
    wx.requestPayment(
      {
      'timeStamp': this.data.timeStamp,
      'nonceStr': this.data.nonceStr,
      'package': this.data.package,
      'signType': this.data.signType,
      'paySign': this.data.paySign,
      'success':(res)=>{
         this.setData({
           show4:true
         })
     
       
        
    console.log(2222,code);
    
      this.getxunhuan2(code)

         
            
             
        
      },
      
      })
  },
  getxunhuan2(code){
    setTimeout(()=>{
      wx.request({
        url: 'https://zujieba.com/Api/Test2/test_print',
        data:{code},
        method:'POST',
        success:(res2)=>{
          console.log(res2);
          var title
        if(res2.data.data.task_state=='READY'){
           title='排队中'
        }else if(res2.data.data.task_state=='PARSING'){
           title='解析中'
        }else if(res2.data.data.task_state=='SENDING'){
          title='发送中'
        }else if(res2.data.data.task_state=='SUCCESS'){
          title='成功'
        }else if(res2.data.data.task_state=='FAILURE'){
          title='失败'
        }
        wx.showLoading({
          title: title,
        })
        console.log(res2);
        if(res2.data.data.task_state=='SUCCESS'){
          wx.hideLoading()
          this.setData({flag:false,show4:false})
          // this.getlist(uid,2,1,10)
          
            this.dian1(3)
         
         
        }else if(res2.data.data.task_state=='FAILUER'){
          wx.hideLoading()
          this.setData({flag:false,show4:false})
        }else{
          this.getxunhuan2(code)
        }
  
  
        }
      })
    },1000)
  },
  // getxunhuan1(code){
  //    wx.request({
  //      url: 'https://cloud.wisiyilink.com/api/print/job',
  //      method:'GET',
  //      header:{Apikey:'ueQr4lbU5lurtgz6D0xilLMcHErzDBOb'},
  //      data:{deviceId,deviceKey,task_id,devicepost},
  //      success:(res2)=>{
  //        console.log(res2);
  //        var title
  //     if(res2.data.data.task_state=='READY'){
  //        title='排队中'
  //     }else if(res2.data.data.task_state=='PARSING'){
  //        title='解析中'
  //     }else if(res2.data.data.task_state=='SENDING'){
  //       title='发送中'
  //     }else if(res2.data.data.task_state=='SUCCESS'){
  //       title='成功'
  //     }else if(res2.data.data.task_state=='FAILURE'){
  //       title='失败'
  //     }
  //     wx.showLoading({
  //       title: title,
  //     })
  //     console.log(res2);
  //     if(res2.data.data.task_state=='SUCCESS'){
  //       wx.hideLoading()
  //       this.setData({flag:false,show4:false})
  //       // this.getlist(uid,2,1,10)
        
  //         this.dian1(3)
       
       
  //     }else if(res2.data.data.task_state=='FAILUER'){
  //       wx.hideLoading()
  //       this.setData({flag:false,show4:false})
  //     }else{
  //       this.getxunhuan1(task_id,deviceId,deviceKey,devicepost)
  //     }
  //      }
  //    })

  // },
  getpaylist(e){
  let code= e.currentTarget.dataset.code
    console.log(e.currentTarget.dataset.type1,code);
    this.setData({total:e.currentTarget.dataset.total,code})
    if(e.currentTarget.dataset.type1==1){
    wx.request({
      url: 'https://zujieba.com/Api/order/pay_order.html',
      data:{code:code},
      success:(res)=>{
        console.log(res);
        this.setData({
          appId:res.data.appId,
          nonceStr:res.data.nonceStr,
          package:res.data.package,
          paySign:res.data.paySign,
          signType:res.data.signType,
          timeStamp:res.data.timeStamp,
          
        })
        this.getpay(code)
        console.log(code);
        
        
      }
    })}else if(e.currentTarget.dataset.type1==2||e.currentTarget.dataset.type1==3){this.setData({show:true})}
  },
   //立即打印
  /* canprint(e){
    let code= e.currentTarget.dataset.code
    wx.navigateTo({
      url: '/pages/canprint/canprint?code='+code,
    })
 },*/
 //触底刷新
 onReachBottom:function(){
   wx.showToast({
     title:'正在加载更多数据',
     icon:"loading"
   })
   success:(res)=>{
     console.log(11);
   }
   let page=this.data.page
   page=page+1
   
   this.getlist(uid,this.data.order,page,this.data.limit)
   this.setData({page:page})
   console.log(this.data.page);
 },
 //去退款
   refund(e){
     wx.navigateTo({
       url: '../mine/refund/refund?code='+e.currentTarget.dataset.code+"&title="+e.currentTarget.dataset.title+"&total="+e.currentTarget.dataset.total+'&dmcopies='+
       e.currentTarget.dataset.dmcopies+'&sheets='+e.currentTarget.dataset.sheets+'&addtime='+e.currentTarget.dataset.addtime,
     })
   },
   giveup(e){
        wx.request({
          url: 'https://zujieba.com/Api/order/del_order.html',
          data:{code:e.currentTarget.dataset.code},
          method:'POST',
          success:(res)=>{
            console.log(res);
            if(res.data.success==1){
              wx.showToast({
                title: res.data.tips,
                duration:2000,
                icon:'success'
              })
              this.getlist(uid,this.data.order,1,this.data.limit)
            }
            
          }
        })
   },
   close(){
    this.setData({
      show:false
    })
   
  },
  input(e){
    let str
    if(e.detail.value.length>6){
     
   let value5= e.detail.value.substring(0,6) 
   this.setData({value5})
    }else{
      str=e.detail.value
      this.setData({value5:str})
    }
    console.log(str)
    
    
    let arr=  str.split('')

    console.log(arr);
    
      this.setData({
        passwordvalue:arr
      })
  },
  sureclick(){
   
   
    console.log(this.data.code);
    
      wx.request({
        url: 'https://zujieba.com/Api/order/pay_order.html',
        data:{paypass:this.data.value5,code:this.data.code},
        method:'POST',
        success:(res)=>{
          if(res.data.success==1){
          console.log(res);
          

            wx.showToast({
              title: res.data.tips,
              duration:2000,
              icon:'success'
            })
            wx.navigateTo({
              url: '/pages/canprint/canprint?code='+this.data.code,
            })
           
            
              this.setData({value5:'',passwordvalue:[]})
              
          }else{
            console.log(res,this);
            
            wx.showToast({
              title: res.data.tips,
              duration:2000
            })
            this.setData({value5:'',passwordvalue:[]})
            console.log(this.data.value5);
            
          }
         
          
        },
        fail:(res)=>{
          console.log(res);
          
          wx.showToast({
            title: res.data.tips,
            duration:2000
          })
          this.setData({value5:''})
        }
      })
  },
  //获取用户的余额
  getyuemoney(){
    wx.request({
      url: 'https://zujieba.com/Api/member/member.html',
      data:{uid:wx.getStorageSync('uid')},
      method:'POST',
      success:(res)=>{
        console.log(res);
        this.setData({
           money:res.data.data.price
        })
        
      }
    })
  },
  onUnload: function () {
    wx.reLaunch({
      url: '/pages/mine/mine',
    })
  
  
  },
  seewuliu(e){
    wx.navigateTo({
      url: '/pages/mine/store_admin/express?code='+e.currentTarget.dataset.code,
    })
},
canprint(e){
  this.setData({flag:true})
  this.throttle(()=>{let code=e.currentTarget.dataset.code
    console.log(2222);
    
    wx.showLoading({
      title: '正在打印,请稍后',
      mask:true
    })
    wx.request({
      url: 'https://zujieba.com/api/Test2/print.html',
      data:{type:1,status:1,code},
      success:(res1)=>{
        
        
        if(res1.data.success==1){
          wx.request({
            url: 'https://zujieba.com/api/Test2/print.html',
            data:{type:2,status:1,code},
            success:(res)=>{
              wx.hideLoading()
              if(res.data.success==1){
                console.log(res);
                
             /*    this.getlist(uid,2,1,10)*/
             let task_id=res.data.data.data.task_id
             let deviceId=res.data.data.order.deviceId
             let deviceKey=res.data.data.order.deviceKey
             this.getxunhuan(task_id,code)
              }else{wx.showToast({
                 title: res.data.tips,
                 icon:'none'
               })}
            }})
          
        }else{
          wx.hideLoading(
                 
            )
          wx.showToast({
            title: res1.data.tips,
            icon:'none'
          })
        }
        
      }
    })
         },1500)
},
//节流
throttle:function(func,wait){
  console.log(func,wait);
  
  let last =this.data.last
  console.log(last);
  
  let current_time= +new Date()
  if(current_time-last>wait){
    console.log(1111);
      console.log(current_time,current_time-last);
      
      func()
      last=+new Date()
      this.setData({
        last
      })
  }
    
     
  
   
},
canprint11(e){
     wx.navigateTo({
       url: '/pages/canprint/canprint?code='+e.currentTarget.dataset.code,
     })
},

getxunhuan(task_id,code){
 setTimeout(()=>{
  wx.request({
    url: 'https://zujieba.com/api/Test2/print.html',
   
    data:{type:3,task_id,code,status:1},
    success:(res2)=>{
       console.log(res2);
      var title
      if(res2.data.data.data.task_state=='READY'){
         title='排队中'
      }else if(res2.data.data.data.task_state=='PARSING'){
         title='解析中'
      }else if(res2.data.data.data.task_state=='SENDING'){
        title='发送中'
      }else if(res2.data.data.task_state=='SUCCESS'){
        title='成功'
      }else if(res2.data.data.data.task_state=='FAILURE'){
        title='失败'
      }
      wx.showLoading({
        title: title,
      })
      console.log(res2);
      if(res2.data.data.data.task_state=='SUCCESS'){
        wx.hideLoading()
        this.setData({flag:false})
        // this.getlist(uid,2,1,10)
        
          this.dian1(3)
       
       
      }else if(res2.data.data.data.task_state=='FAILUER'){
        wx.hideLoading()
        this.setData({flag:false})
      }else{
        this.getxunhuan(task_id,code)
      }
     
    }
  })
 },1000)
},
/*getorder(code){
  wx.request({
    url: 'https://zujieba.com/Api/Test2/change_order',
    data:{code},
   method:'POST',
   success:(res)=>{
     console.log(res);
     if(res.data.success==1){
       this.setData({flag:false})
      this.getlist(uid,2,1,10)
     }
     
   }
  })
},*/
onHide:function(){
    this.setData({show:false})
},
down(e){
  console.log(111);
  
  wx.getImageInfo({
    src: e.currentTarget.dataset.file,
    success:(res)=>{
      console.log(res);
      wx.saveImageToPhotosAlbum({
        filePath: res.path,
        success:(res1)=>{
          console.log(res1);
          
        }
      })
      
    }
  })
}
  

})